<template>
    <view class="pages"> 

        <my-header bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{longitude}}地图找房</block>
		</my-header>
        <map v-if="longitude" :longitude="longitude" :latitude="latitude" :markers="covers" @markertap="go"></map>  
        <!-- <view class="col">
            <view class="btn bg-cyan" @click="add()">返回</view>
        </view> -->
        <open-setting v-if="!longitude" tips="您拒绝了获取您的坐标，暂无法使用该功能，请前往设置开放该权限" ></open-setting>
        <loading v-if="!covers[0].iconPath"></loading>

    </view>
</template>

<script>
    const app = getApp()
	export default {
		data() {
			return {
                longitude:false,
                latitude:"",
                covers:[
                    {
                        id:0,
                        title:"当前位置",
                        latitude:"",
                        longitude:""
                    }
                ]
			}
        },
        onLoad() {
            this.initData()
        },
        onShow(){
			this.get_local()
        },
		methods: {
            async initData(){
                const res= await uni.$request({},"house/index/mapHouse")
                if (res.code==200) {
                    let covers=[
                        ...this.covers,
                        ...res.data.covers
                    ]
                    for (let i = 0; i < covers.length; i++) {
                        const element = covers[i];
                        element.width=res.data.width
                        element.height=res.data.height
                        element.iconPath=i==0?res.data.from:res.data.mark
                    }
                    this.covers=covers
                }
            },
           async go(e){
                if (e.detail.markerId>0) {
                   const res=await uni.$choose("您确认要前往该房源吗")
                   if (res.code==200) {
                       uni.navigateTo({
                         url: '/pages/house_info/house_info?id='+e.detail.markerId
                    });
                   }
                    
                }else{
                    console.log("跳转");
                }
                console.log(e);
            },
            is_local(){
                uni.authorize({
                    scope:"scope.userLocation",
                    success:(res)=>{
                        console.log(res);
                    },
                    fail:(err)=>{
                    console.log(err); 
                        
                    }
                })
            },
			get_local(){
                uni.getLocation({
                type:"gcj02",
                altitude:true,
                geocode:true,
                success:(res)=>{
                    this.latitude=res.latitude
                    this.longitude=res.longitude
                    this.covers[0].latitude=res.latitude
                    this.covers[0].longitude=res.longitude

                },
                fail:(err)=>{
                   console.log(err); 
                    
                }

            })
            }
		
		},
		
	}
</script>
<style lang="scss">
    map{
        width: 100%;
        height: 100%;
    }
</style>
